package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaView;
import com.wudl.govern.service.MetaViewService;
import com.wudl.govern.vo.MetaViewVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * meta_view Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaView")
public class MetaViewController {

    @Autowired
    private MetaViewService metaViewService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaViewVO>> page(@RequestBody MetaViewVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaViewVO> MetaViewVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaView> MetaViewList = metaViewService.page(page, size);

        for (MetaView metaViewTmp:MetaViewList )
        {
            MetaViewVO metaViewVO = new MetaViewVO();
            BeanUtil.copyProperties(metaViewTmp, MetaViewVO.class);
            MetaViewVoList.add(metaViewVO);
        }
        return CommonResult.success(MetaViewVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaViewVO> get(@PathVariable Object id) {
        MetaView entity = metaViewService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaViewVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaViewVO addVO) {
        metaViewService.save(BeanUtil.copyProperties(addVO, MetaView.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaViewVO editVO) {
        metaViewService.updateById(BeanUtil.copyProperties(editVO, MetaView.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaViewService.removeById(id);
        return CommonResult.success();
    }

}
